package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"html/template"
	"src/gitee.com/wx_e6924dbcd0/notebook/models"
	"strconv"
)

type CriMainController struct {
	beego.Controller
}

/**
 * 获取评论信息根据文章Id
 */
//http://127.0.0.1:8080/crilist?message_id=2
type RdsData struct {
	Id          int    `json:"Id"`
	Message_id  int    `json:"Message_id"`
	Send_name   string `json:"Send_name"`
	Content     string `json:"Content"`
	Accept_name string `json:"Accept_name"`
	Status      int    `json:"Status"`
	Send_time   string `json:"Send_time"`
	Support     int    `json:"Support"`
	Oppose      int    `json:"Oppose"`
}

func (c *CriMainController) CriList() {
	//xss
	m_id := template.HTMLEscapeString(c.Input().Get("message_id"))
	message_id, errm := strconv.Atoi(m_id)
	if errm != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errm.Error())
		return
	}
	result := make(map[string]interface{})
	//先从redis服务器获取评论列表
	RedisData, err := models.GetRedis(m_id)
	if err != nil {
		fmt.Println("Redis 查询错误", err)
	}
	if len(RedisData) > 0 {
		var data []RdsData
		var rdsData RdsData
		//将[]interface{}、[]string类型的数据存入结构体struct
		for _, value := range RedisData {
			//fmt.Printf("redis list :%d=> %s | %v", key, value, value)
			rdsDataString := fmt.Sprintf("%s", value)
			err := json.Unmarshal([]byte(rdsDataString), &rdsData)
			if err != nil {
				fmt.Printf("json.Unmarshal err :%s,  %s", err.Error(), value)
			}
			data = append(data, rdsData)
		}
		result["CommentList"] = data
		c.Data["json"] = result
		c.ServeJSON()
		return
	}
	//若redis服务器没有数据则从MySql服务器获取评论列表，并将数据存进redis服务器，过期时间为5分钟
	CommentList, err := models.GetComment(int64(message_id))
	if err != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + err.Error())
		return
	}
	//将数据存进redis服务器，过期时间为5分钟
	//string(jsonStu)
	for i, onedata := range CommentList {
		//fmt.Println(i, onedata)
		jsonStu, err := json.Marshal(onedata)
		//fmt.Println(string(jsonStu), i)
		if err != nil {
			//fmt.Println("生成json字符串错误")
			log := models.UitlLog()
			log.Error("生成json字符串错误" + err.Error())
			return
		}
		errr := models.SetRedis(m_id, i, jsonStu)
		if errr != nil {
			//fmt.Println("生成json字符串错误")
			log := models.UitlLog()
			log.Error("Redis存储错误" + errr.Error())
			return
		}
	}
	result["CommentList"] = CommentList
	c.Data["json"] = result
	c.ServeJSON()
	return
}

/**
 *  获取评论信息根据用户名
 */
//http://127.0.0.1:8080/crilistapt?user_name=2
func (c *CriMainController) CriListApt() {
	//xss
	user_name := template.HTMLEscapeString(c.Input().Get("user_name"))
	if user_name != "" {
		result := make(map[string]interface{})
		ListMId, err := models.GetListMId_Apt(user_name)
		if err != nil {
			log := models.UitlLog()
			log.Error("db GetListMId_Apt err" + err.Error())
			return
		}
		//获取点赞列表
		thumbupList, _ := models.GetThumbList()
		result["thumbupList"] = thumbupList
		result["List_Apt"] = ListMId
		c.Data["json"] = result
		c.ServeJSON()
		return
	}

}

/**
 * 获取评论信息根据用户名,只要不重复Message_id的对象,
 */
//http://127.0.0.1:8080/crilistname?send_name=ii
func (c *CriMainController) CriListName() {
	send_name := template.HTMLEscapeString(c.Input().Get("send_name"))
	result := make(map[string]interface{})
	ListMId, err := models.GetListMId(send_name)
	if err != nil {
		log := models.UitlLog()
		log.Error("GetListMId err" + err.Error())
		return
	}
	result["ListMId"] = ListMId
	c.Data["json"] = result
	c.ServeJSON()
	return
}

/**
 * 评论
 */
//http://127.0.0.1:8080/comment?message_id=1&send_name=2&accept_name=3&content="你说的都是"
func (c *CriMainController) DoComment() {
	m_id := template.HTMLEscapeString(c.Input().Get("message_id"))
	message_id, errm := strconv.Atoi(m_id)
	if errm != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errm.Error())
		return
	}
	send_name := c.Input().Get("send_name")
	accept_name := c.Input().Get("accept_name")
	content := c.Input().Get("content")
	check := c.Input().Get("check")
	if content != "" && send_name != "" && accept_name != "" && check != "" {
		// orm insert 存储
		OneCri, err := models.Comment(int64(message_id), send_name, accept_name, content, check)
		if err != nil {
			log := models.UitlLog()
			log.Error("Comment err 查询错误" + err.Error())
			return
		}
		c.Data["json"] = OneCri
		c.ServeJSON()
	}
	return
}

/**
 * 根据评论ID增加反对数
 */
//http://127.0.0.1:8080/oppose?c_id=2
func (c *CriMainController) Oppose() {
	id := template.HTMLEscapeString(c.Input().Get("c_id"))
	C_id, errc := strconv.Atoi(id)
	if errc != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errc.Error())
		return
	}
	_, err := models.AddOppose(int64(C_id))
	if err != nil {
		log := models.UitlLog()
		log.Error("AddOppose err" + err.Error())
		return
	}
	c.Data["json"] = "成功！"
	c.ServeJSON()
	return
}

/**
 * 根据评论ID减少反对数
 */
//http://127.0.0.1:8080/unoppose?c_id=2
func (c *CriMainController) UnOppose() {
	id := template.HTMLEscapeString(c.Input().Get("c_id"))
	C_id, errc := strconv.Atoi(id)
	if errc != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errc.Error())
		return
	}
	_, err := models.DelOppose(int64(C_id))
	if err != nil {
		log := models.UitlLog()
		log.Error("DelOppose err" + err.Error())
		return
	}
	c.Data["json"] = "成功！"
	c.ServeJSON()
	return
}

/**
 * 根据评论ID增加支持数
 */
//http://127.0.0.1:8080/support?c_id=2
func (c *CriMainController) Support() {
	id := template.HTMLEscapeString(c.Input().Get("c_id"))
	C_id, errc := strconv.Atoi(id)
	if errc != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errc.Error())
		return
	}
	_, err := models.AddSupport(int64(C_id))
	if err != nil {
		log := models.UitlLog()
		log.Error("AddSupport err" + err.Error())
		return
	}
	c.Data["json"] = "成功！"
	c.ServeJSON()
	return
}

/**
 * 根据评论ID减少支持数
 */
//http://127.0.0.1:8080/unsupport?c_id=2
func (c *CriMainController) UnSupport() {
	id := template.HTMLEscapeString(c.Input().Get("c_id"))
	C_id, errc := strconv.Atoi(id)
	if errc != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errc.Error())
		return
	}
	_, err := models.DelSupport(int64(C_id))
	if err != nil {
		log := models.UitlLog()
		log.Error("DelSupport err" + err.Error())
		return
	}
	c.Data["json"] = "成功！"
	c.ServeJSON()
	return
}

/**
 * 根据ID取消评论
 */
//http://127.0.0.1:8080/delcomment?c_id=1
func (c *CriMainController) DelComment() {
	// 读取数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取数据库
	id := template.HTMLEscapeString(c.Input().Get("c_id"))
	//send_name := c.Input().Get("send_name")
	cri_id, errc := strconv.Atoi(id)
	if errc != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errc.Error())
		return
	}
	result := make(map[string]interface{})
	//执行删除操作
	num, err := models.DoDelComment(int64(cri_id))
	if err != nil {
		log := models.UitlLog()
		log.Error("DoDelComment err" + err.Error())
		//失败返回数据
		return
	}
	// 成功返回数据
	result["count"] = num
	result["DelCri"] = "成功！"
	c.Data["json"] = result
	c.ServeJSON()
	return
}

/**
 * 根据评论ID修改状态值
 */
//http://127.0.0.1:8080/upsta?c_id=2&sta=
func (c *CriMainController) UpSta() {
	id := template.HTMLEscapeString(c.Input().Get("c_id"))
	C_id, errc := strconv.Atoi(id)
	if errc != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errc.Error())
		return
	}
	sta_id := c.Input().Get("sta")
	sta, errc := strconv.Atoi(sta_id)
	if errc != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errc.Error())
		return
	}
	err := models.UpStaVal(int64(C_id), int64(sta))
	if err != nil {
		log := models.UitlLog()
		log.Error("UpStaVal failed err" + err.Error())
		return
	}
	c.Data["json"] = "成功！"
	c.ServeJSON()
	return
}

/**
 *  获取评论信息Check值为0的评论
 */
//http://127.0.0.1:8080/message/list/uncheck
func (c *CriMainController) GetUnCheckComment() {
	result := make(map[string]interface{})
	List, err := models.UnCheckComment()
	if err != nil {
		log := models.UitlLog()
		log.Error("db CheckComment err" + err.Error())
		return
	}
	result["List"] = List
	c.Data["json"] = result
	c.ServeJSON()
	return
}

/**
 * 根据评论ID修改Check值
 */
//http://127.0.0.1:8080/message/check?id=20
func (c *CriMainController) DoEditCheck() {
	id := template.HTMLEscapeString(c.Input().Get("id"))
	C_id, errc := strconv.Atoi(id)
	if errc != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errc.Error())
		return
	}
	err := models.EditCheck(int64(C_id))
	if err != nil {
		log := models.UitlLog()
		log.Error("UpStaVal failed err" + err.Error())
		return
	}
	c.Data["json"] = "成功！"
	c.ServeJSON()
	return
}
